<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>流式数据接收演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        #container {
            max-width: 800px;
            margin: 0 auto;
        }
        #status {
            color: #666;
            margin-bottom: 20px;
        }
        #output {
            border: 1px solid #ddd;
            padding: 15px;
            min-height: 200px;
            overflow-y: auto;
            background-color: #f9f9f9;
        }
        .step {
            margin: 10px 0;
            padding: 8px;
            background-color: white;
            border-radius: 4px;
        }
        .loading { background-color: #e3f2fd; }
        .error { background-color: #ffebee; color: #ff5252; }
    </style>
</head>
<body>
<div id="container">
    <h1>流式数据接收演示</h1>
    <div id="status">连接中...</div>
    <button onclick="startStream()">开始接收数据</button>
    <div id="output"></div>
</div>

<script>
        async function startStream() {
            const outputElement = document.getElementById('output');
            outputElement.innerHTML = ''; // 清空旧数据
            const statusElement = document.getElementById('status');

            try {
                // 发起 GET 请求
                const response = await fetch('/test/stream', {
                    method: 'GET',
                    headers: { 'Accept': 'text/plain' },
                });

                if (!response.ok) {
                    throw new Error(`HTTP 错误! 状态码: ${response.status}`);
                }

                // 处理流式数据
                const reader = response.body.getReader();
                const decoder = new TextDecoder();

                statusElement.textContent = "数据接收中...";
                statusElement.style.color = "#2196F3";

                // 逐块读取数据
                function readStream() {
                    return reader.read().then(({ done, value }) => {
                        if (done) {
                            statusElement.textContent = "传输完成！";
                            statusElement.style.color = "#4CAF50";
                            return;
                        }

                        // 解码并显示数据
                        const chunk = decoder.decode(value, { stream: true });
                        outputElement.innerHTML += `<div class="step loading">${chunk}</div>`;
                        outputElement.scrollTop = outputElement.scrollHeight; // 自动滚动到底部

                        // 模拟每秒接收一次数据（根据服务端响应间隔调整）
                        return new Promise(resolve =>
                            setTimeout(resolve, 1000)
                        ).then(readStream);
                    });
                }

                readStream().catch(error => {
                    statusElement.textContent = `错误: ${error.message}`;
                    statusElement.style.color = "#ff5252";
                    outputElement.innerHTML = `<div class="step error">传输失败: ${error.message}</div>`;
                });

            } catch (error) {
                statusElement.textContent = `请求失败: ${error.message}`;
                statusElement.style.color = "#ff5252";
            }
        }
    </script>
</body>
</html>